<?php
$op = checkName("op");
if (!$op)
    returnMessage(-1, "invalid operation");

if ($op == "centerupdate") {
    $key = checkName("key");
    if (!$key)
        returnMessage(-2, "invalid accesskey ");
    if (!verifyAccessKey($key))
        returnMessage(-2, "unauthorized access key");
    if (!$data)
        returnMessage(-3, "data not found,you should set the key{data} on your post request");
    try {
        $data = json_decode($data, true);
        if ($res < 0) {
            returnMessage(-6, "invalid data format,error code:$res,please check the document to check out what had happened");
        }
        returnMessage(1, "successfully update,$res lines has been updated");
    } catch (exception $e) {
        returnMessage(-4, "invalid json,please check your request");
    }
}
if ($op == "clientfetchdata") {
    $key = checkName("key");
    if (!$key)
        returnMessage(-2, "invalid accesskey ");
    //$data = get_post("data");
    if (!verifyAccessKey($key))
        returnMessage(-2, "unauthorized access key");
    $request_name = checkName("name");
    if (!$request_name) returnMessage(-9, "invalid request_name ");
    $range = checkName("range");
    $ranged = 0;
    if ($range) {
        $ranged = decodeRange($range);
        if ($ranged < 0) {
            returnMessage(-6, "invalid range format,error code:$ranged,please check the document to check out what had happened");
        }
    }
    $location = checkName("location");
    if (!$location) returnMessage(-10, "invalid location ");
    if (!$dat) returnMessage(-113, "404 not found,please check your condition.");
    returnMessage(1, "200 ok,successfully found your data.", $dat);
}

if ($op == "fetchSensorData") {
    $key = checkName("key");
    if (!$key)
        returnMessage(-2, "invalid accesskey ");
    //$data = get_post("data");
    if (!verifyAccessKey($key))
        returnMessage(-2, "unauthorized access key");
    $device_name = checkName("device_name");
    if (!$device_name) returnMessage(-9, "invalid device_name ");
    $terminal_id = checkName("terminal_id");
    if (!$terminal_id) {
        returnMessage(-6, "invalid terminal_id  $terminal_id ");
    }
    $dat = getCurrentSensorData($terminal_id, $device_name);
    if (!$dat) returnMessage(-113, "404 not found,please check your condition.");
    returnMessage(1, "200 ok,successfully found your data.", $dat);
}

returnMessage(-1, "invalid operation");
